package controllers;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import models.Programa;
import play.mvc.Controller;
import play.mvc.With;
import service.ProgramaService;

@With(Secure.class)
public class ProgramaAction extends Controller{

	/**
	 * 查询所有栏目信息的方法
	 */
	@Check("InformationManage")
	public static void list(){
		List<Programa> programas = ProgramaService.findTop();
		render(programas);
	}
	
	@Check("InformationManage")
	public static void sulist(Long id){
		List<Programa> programas = ProgramaService.findByProId(id);
		render(programas);
	}
	/**
	 * 跳转向添加页面的方法
	 */
	@Check("InformationManage")
	public static void blank(){
		List<Programa> programas = ProgramaService.findTop();
		render(programas);
	}
	
	/**
	 * 添加新栏目的方法
	 * 
	 * @param programa  栏目对象
	 * @param parId     父栏目id
	 */
	@Check("InformationManage")
	public static void create(Programa programa,Long[] parId){
		Long id=0l;
		for (Long long1 : parId) {
			if(long1!=0l){
				id = long1;
			}
		}
		try {
			programa.pro = ProgramaService.findById(id);
			ProgramaService.add(programa);
			flash.success("添加成功");
			list();
		} catch (Exception e) {
			System.out.println("##类ProgramaAction中的create（）方法抛异常");
			e.printStackTrace();
			flash.success("添加失败");
			blank();
		}

	}
	
	/**
	 * 查询相关栏目的子栏目的方法
	 * 
	 * @param id
	 */
	@Check("InformationManage")
	public static void test(Long id){
		StringBuffer sb= new StringBuffer();
		sb.append("[");
		List<Programa> programas = ProgramaService.findByProId(id);
		int i = 0;
		int s = programas.size();
		if(s == 0){
			sb.append("{\"id\":\"non\"}]");
		}else{
			for (Programa programa : programas) {
				i++;
				sb.append("{\"id\":\""+programa.id+"\",\"proName\":\""+programa.proName+"\"");
				if(i == s){
					sb.append("}]");
				}else{
					sb.append("},");
				}
			}
		}
		renderJSON(sb.toString());
		//render(ss);
	}
	
	/**
	 * 跳转向修改页面的方法
	 * 
	 * @param id  栏目id
	 */
	@Check("InformationManage")
	public static void show(Long id){
		Programa programa = ProgramaService.findById(id);
		List<Programa> programas = ProgramaService.findTop();
		render(programa,programas);
	}
	
	/**
	 * 修改栏目的方法
	 * 
	 * @param programa  栏目对象
	 * @param id        父栏目id
	 */
	@Check("InformationManage")
	public static void save(Programa programa,Long[] parId){
		Long id=0l;
		for (Long long1 : parId) {
			if(long1!=0l){
				id = long1;
			}
		}
		Programa programa01 = Programa.findById(programa.id);
		programa01.pro = Programa.findById(id);
		programa01.proCode = programa.proCode;
		programa01.proName = programa.proName;
		try {
			ProgramaService.update(programa01);
			flash.success("修改成功");
			List<Programa> programas = Programa.findAll();
			for (Programa programa2 : programas) {
				if(programa2.pro == null){
					programa2.proPath = null;
					ProgramaService.update(programa2);
				}else if(programa2.pro.proPath == null){
					programa2.proPath = programa2.pro.id.toString();
					ProgramaService.update(programa2);
				}else{
					programa2.proPath = programa2.pro.proPath+","+programa2.pro.id;
					ProgramaService.update(programa2);
				}
			}
			list();
		} catch (Exception e) {
			System.out.println("################ProgramaAction.sava() Exception############################");
			e.printStackTrace();
			flash.success("修改失败");
			show(programa.id);
		}

	}
	
	/**
	 * 删除栏目的方法
	 * 
	 * @param id  栏目id
	 */
	@Check("InformationManage")
	public static void delete(Long id){
		Programa par = ProgramaService.findById(id);
		List<Programa> programas = Programa.find("pro.id=?", id).fetch();
		for (Programa programa : programas) {
			programa.pro = par.pro;
			ProgramaService.update(programa);
		}
		programas = Programa.findAll();
		for (Programa programa2 : programas) {
			if(programa2.pro == null){
				programa2.proPath = null;
				ProgramaService.update(programa2);
			}else if(programa2.pro.proPath == null){
				programa2.proPath = programa2.pro.id.toString();
				ProgramaService.update(programa2);
			}else{
				programa2.proPath = programa2.pro.proPath+","+programa2.pro.id;
				ProgramaService.update(programa2);
			}
		}
		try {
			ProgramaService.delete(id);
			flash.success("删除成功");
			list();
		} catch (Exception e) {
			System.out.println("################ProgramaAction.delete() Exception############################");
			e.printStackTrace();
			flash.success("删除失败");
			list();
		}
	}
}
